package com.bafa.front.admin;

import com.bafa.email.Email;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bafa.exceptions.ErrorException;
import com.bafa.objects.Match;
import com.bafa.objects.team.Team;
import com.bafa.services.RetrieveServices;
import com.bafa.services.FixtureServices;

public class RescheduleMatchServlet extends AdminServlet {

	
	@Override
	public void doAdminEvent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ErrorException {

		FixtureServices.rescheduleAFixture(new Integer(request.getParameter("matchId")).intValue(),
				new Integer(request.getParameter("day")).intValue(),
				new Integer(request.getParameter("month")).intValue(),
				new Integer(request.getParameter("year")).intValue(),
				new Integer(request.getParameter("hour")).intValue(),
				new Integer(request.getParameter("minute")).intValue());
	    Match match = RetrieveServices.getMatch(new Integer(request.getParameter("matchId")).intValue());
            Team teamA = RetrieveServices.getTeam(match.getTeamA()+"");
	    Team teamB = RetrieveServices.getTeam(match.getTeamB()+"");
                String[] emails = {teamA.getPrimaryContactEmail(),teamA.getSecondaryContactEmail(),teamB.getPrimaryContactEmail(),teamB.getSecondaryContactEmail()};
	    
	    StringBuffer body = new StringBuffer();
	    body.append("This email is to confirm that the match between ");
	    body.append(teamA.getName());
	    body.append(" and ");
	    body.append(teamB.getName()+", has been rescheduled to  ");
	    body.append(match.dateOfMatch()+" at ");
	    body.append(request.getParameter("hour")+":"+request.getParameter("minute")+". \n");
	    body.append("If you have any queries please contact bafa.bafa@ba.com");
	    Email.sendEmail("bafa.bafa@ba.com",emails,"Confirmation of match date/time change",body.toString());
	url = "logon.bafa";
		
	}
}
